


import 'package:myapp/http/core/net_response.dart';


enum HttpMethod { GET, POST }


abstract class BaseRequest {

  String path();

  HttpMethod httpMethod();


  final Map<String, String?> _headers = {};

  //queryParams
  final Map<String, dynamic> _queryParams = {};

  //jsonBody
  final Map<String, dynamic> _bodyRootParams = {};

  String url() {

    StringBuffer urlBuffer = StringBuffer();
    urlBuffer.write(path());
    if (_queryParams.isNotEmpty) {
      urlBuffer.write("?");
      _queryParams.forEach((key, value) {
        urlBuffer.write(key);
        urlBuffer.write('=');
        urlBuffer.write(value);
        urlBuffer.write('&');
      });
    }

    return urlBuffer.toString();
  }

  BaseRequest addHeader(String k, String? v) {
    _headers[k] = v;
    return this;
  }

  Map<String, String?> getHeaders() {
    return _headers;
  }

  BaseRequest addQueryParams(String k, Object v) {
    _queryParams[k] = v;
    return this;
  }


  BaseRequest addRequestBody(String k, Object v) {
    _bodyRootParams[k] = v;
    return this;
  }

  Map<String, dynamic> getRequestBody() {
    return _bodyRootParams;
  }



  void onAsyncFinish();

  void onAsyncSuccess(dynamic netResult);

  void onAsyncFail(String code,String msg);


  Future<NetResponse<T>> onSyncSuccess<T>(dynamic response);

  Future<NetResponse<T>> onSyncFail<T>(String code,String msg);

  void onSyncFinish();
}
